﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows;

namespace SpriteExtractor.ViewModel
{
    public class ImageViewModel : ViewModelBase
    {
        public Model.Image Image { get; protected set; }

        public string Path { get { return Image.Path; } set { Image.Path = value; OnPropertyChanged("Path"); } }
        
        public Size Size { get { return Image.Size; } set { Image.Size = value; OnPropertyChanged("Size"); } }
        
        int _scale = 1;
        public int Scale { get { return _scale; } set { _scale = value; OnPropertyChanged("Scale"); } }

        public ReadOnlyObservableCollection<SpriteViewModel> Sprites 
        { 
            get 
            {
                var spriteViewModels = new ObservableCollection<SpriteViewModel>();
                Image.Sprites.ForEach(sprite => spriteViewModels.Add(new SpriteViewModel(this, sprite)));
                return new ReadOnlyObservableCollection<SpriteViewModel>(spriteViewModels); 
            } 
        }

        public ImageViewModel(Model.Image image)
        {
            Image = image;
        }

        #region Commands

        public CommandViewModel AddSpriteCommand 
        {
            get 
            {
                return new CommandViewModel("_Add Sprite", new RelayCommand(param=>this.AddSprite()));
            }
        }
        public CommandViewModel RemoveSpriteCommand
        {
            get
            {
                return new CommandViewModel("_Remove Sprite", new RelayCommand(sprite => this.RemoveSprite(sprite as SpriteViewModel)));
            }
        }

        private void AddSprite()
        {
            Image.Sprites.Add(new Model.Sprite());
            OnPropertyChanged("Sprites");
        }
        private void RemoveSprite(SpriteViewModel spriteViewModel)
        {
            Debug.Assert(spriteViewModel != null);
            Debug.Assert(spriteViewModel.Sprite != null);

            Image.Sprites.Remove(spriteViewModel.Sprite);
            OnPropertyChanged("Sprites");
        }

        #endregion
    }
}
